package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @version v1.0
 * @package com.itheima.service
 * @description:
 * @author: Ewaits
 * @date: 2019/7/2 19:04
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 根据用户名查询用户
     *
     * @param username
     * @return
     */
    @Override
    public User findUserByUsername(String username) {

        User user = userDao.findUserByUsername(username);
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findRolesByUserId(userId);
        if (!CollectionUtils.isEmpty(roles)) {
            roles.forEach((role)->{
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findPermissionsByRoleId(roleId);
                if (!CollectionUtils.isEmpty(permissions)) {
                    role.setPermissions(permissions);
                }
            });
            user.setRoles(roles);
        }
        return user;
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userDao.pageQuery(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(User user, Integer[] roleIds) {
        userDao.add(user);
        setRelationUserAndRole(user.getId(), roleIds);
    }

    private void setRelationUserAndRole(Integer id, Integer[] roleIds) {
        if (roleIds != null && roleIds.length > 0) {
            List<Map<String, Integer>> list = new ArrayList<>();
            for (Integer roleId : roleIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("user_id", id);
                map.put("role_id", roleId);
                list.add(map);
            }
            userDao.setRelationUserAndRole(list);
        }
    }

    @Override
    public void deleteUserById(Integer id) {
        userDao.deleteRoleIdsByUserId(id);
        userDao.deleteUserById(id);
    }

    @Override
    public User findUserById(Integer id) {
        return userDao.findUserById(id);
    }

    @Override
    public Integer[] findRoleIdsByUserId(Integer id) {
        return userDao.findRoleIdsByUserId(id);
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        userDao.update(user);
        userDao.deleteRoleIdsByUserId(user.getId());
        setRelationUserAndRole(user.getId(),roleIds);
    }
}
